package com.gibs.peixunapi.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import javax.validation.Valid;

import org.springframework.transaction.annotation.Transactional;
import com.gibs.peixunapi.result.PageData;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.utils.ResultUtil;
import com.gibs.peixunapi.dao.NoticeDao;
import com.gibs.peixunapi.service.NoticeService;
import com.gibs.peixunapi.model.Notice;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService{

	@Autowired
	private NoticeDao oticeDao;

	@Transactional
@Override
	public Result create(@Valid Notice object) {
		try {
		
//			objectDao.save(object);
			return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Transactional
@Override
	public Result save(@Valid Notice object) {
		try {
			
//			Notice notice = objectDao.findOne(object.getId());
//			ConverterUtil.copyNotNullProperties(object,notice);
//			objectDao.save(notice);
			return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result delete(Integer id) {
		try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				objectDao.delete(Integer.valueOf(str));
//			}
			return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


@Override
	public Result  get(Integer id) {
		try {
//			HashMap<String, Object> data = new HashMap<String, Object>();
//			Notice object = objectDao.findOne(id);
//			if (object == null) {
//				Result(false, Status.FAILD, "查找失败", null);
//			}
//			HashMap<String,Object> map = new HashMap();
//			map.put("id", object.getId());
//			map.put("title", object.getTitle()==null?"":object.getTitle());
//			HashMap<String,Object> sender = new HashMap<String,Object>();
//			sender.put("name", object.getSender()==null?"":object.getSender().getName());
//			sender.put("id", object.getSender()==null?"":object.getSender().getId());
//			map.put("sender", sender);
//			HashMap<String,Object> accepter_id = new HashMap<String,Object>();
//			accepter_id.put("name", object.getAccepter_id()==null?"":object.getAccepter_id().getName());
//			accepter_id.put("id", object.getAccepter_id()==null?"":object.getAccepter_id().getId());
//			map.put("accepter_id", accepter_id);
//			map.put("sendDate", object.getSendDate()==null?"":DateUtils.toString(object.getSendDate()));
//			map.put("content", object.getContent()==null?"":object.getContent());
//			data.put("object",map);

			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	public Result  getList(String key) {
		try {
//			List<Notice> list = new ArrayList<>();
//			if (!StringUtils.isEmpty(key)) {
//				list = objectDao.findLike("%" + key + "%");
//			} else {
//				list = objectDao.findAll();
//			}
//			if (list.size() == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(Notice object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				map.put("title", object.getTitle()==null?"":object.getTitle());
//				HashMap<String,Object> sender = new HashMap<String,Object>();
//				sender.put("name", object.getSender()==null?"":object.getSender().getName());
//				sender.put("id", object.getSender()==null?"":object.getSender().getId());
//				map.put("sender", sender);
//				HashMap<String,Object> accepter_id = new HashMap<String,Object>();
//				accepter_id.put("name", object.getAccepter_id()==null?"":object.getAccepter_id().getName());
//				accepter_id.put("id", object.getAccepter_id()==null?"":object.getAccepter_id().getId());
//				map.put("accepter_id", accepter_id);
//				map.put("sendDate", object.getSendDate()==null?"":DateUtils.toString(object.getSendDate()));
//				map.put("content", object.getContent()==null?"":object.getContent());
//				data.add(map);
//			}
			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result getListPage(String key, Integer current, Integer limit) {
		try {
//			List<Notice> list = new ArrayList<>();
//			limit = limit == null ? 10 : limit;
//			current = current == null ? 1 : current;
//			int offset = (current - 1) * limit;
//			Long total = null;
//			if (!StringUtils.isEmpty(key)) {
//				list = objectDao.findPageLike("%" + key + "%", offset, limit);
//				total = objectDao.countLike("%" + key + "%");
//			} else {
//				list = objectDao.findPage(offset, limit);
//				total = objectDao.count();
//			}
//			if (total == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(Notice object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				map.put("title", object.getTitle()==null?"":object.getTitle());
//				HashMap<String,Object> sender = new HashMap<String,Object>();
//				sender.put("name", object.getSender()==null?"":object.getSender().getName());
//				sender.put("id", object.getSender()==null?"":object.getSender().getId());
//				map.put("sender", sender);
//				HashMap<String,Object> accepter_id = new HashMap<String,Object>();
//				accepter_id.put("name", object.getAccepter_id()==null?"":object.getAccepter_id().getName());
//				accepter_id.put("id", object.getAccepter_id()==null?"":object.getAccepter_id().getId());
//				map.put("accepter_id", accepter_id);
//				map.put("sendDate", object.getSendDate()==null?"":DateUtils.toString(object.getSendDate()));
//				map.put("content", object.getContent()==null?"":object.getContent());
//				data.add(map);
//			}
//			PageData pageData = new PageData(total, limit, current);
			return ResultUtil.successPage(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	

@Override
	public Result getOptions(String key) {
		try {
//			List<Notice> list = new ArrayList<>();
//			if (!StringUtils.isEmpty(key)) {
//				list = objectDao.findLike("%" + key + "%");
//			} else {
//				list = objectDao.findAll();
//			}
//			List<Object> data = new ArrayList<>();
//			for (Notice object : list) {
//				HashMap<String, Object> map = new HashMap();
//				map.put("value", object.getId());
//				map.put("name", object.getTitle());
//				data.add(map);
//			}
			return ResultUtil.success(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


}

